<?php   
	session_start();

	if (!isset($_SESSION['login']) && !$_SESSION['login'] == true) {
		header ("Location: index.php");
	}
	if(isset($_SESSION['SeeHidden']))
		$seeHidden = $_SESSION['SeeHidden'];
	else
	{
		$seeHidden = 0;
		$_SESSION['SeeHidden'] = 0;
	}
		
	include("LangHeader.php");
	include 'config.php';
	include("sendmail.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="scripts/jquery.tablesorter.js"></script>
<script src="scripts/salesmakeover.js"></script>
<title>Sales Makeover</title>
</head>

<body>


	<div class="container_head">
		<?php include("header.html"); ?>
	</div>
	
	<div class="container" id="container">
		<div class="content">
			<?php
				$userroleToShow = 2;
				if(isset($_SESSION['userroleid']))
				{
					switch($_SESSION['userroleid'])
					{
						case 1:
							$userroleToShow = 2;
							echo "<span><div style='width:100%;height:40px;'><div class='button' style='float:right;' onclick=\"javascript:location.href='add.php'\"><div class=\"tr\" key=\"NewCompany\">Lägg till företag</div></div></div></span>";
						break;
						
						case 2:
							$userroleToShow = 3;
							echo "<span><div style='width:100%;height:40px;'><div class='button' style='float:right;' onclick=\"SalesMakeover.hideDiv('form_editEmployee',false);\"><div class=\"tr\" key=\"NewUser\">Ny användare</div></div></div></span>";
						break;
						
						default:
						break;
					}
				} 
				echo "<span><div style='width:100%;height:40px;'><div style='float:right;'><label for='seeHidden' class='tr' key='See_Hidden'></label><input type='checkbox' id='seeHidden' name='seeHidden' onclick='SalesMakeover.clickHidden(this);' /></div></div></span>";
			?>
			<div id="mainHolder" style="overflow-x: hidden;">
			<table border="0" id="myTable" class="tablesorter">
				<thead>
					<tr>
						<th class="name tr_add " key="Company" ><img style='margin-left:5px;' src="content/icons/filter_arrows.png"></th> 
						<th class="business tr_add" key="Name"><img style='margin-left:5px;' src="content/icons/filter_arrows.png"></th>
						<th class="email tr_add" key="Email"><img style='margin-left:5px;' src="content/icons/filter_arrows.png"></th>
						<?php if($userroleToShow == 3) { echo "<th class=\"buttons tr\" key=\"Show\"></th>";}?>
						<th class="buttons tr" key="Edit" style=></th>
						<?php 
							if($seeHidden == "1") 
								echo "<th class=\"buttons tr\" key=\"Activate\"></th>";
							else
								echo "<th class=\"buttons tr\" key=\"Activate\"></th>";
								
								?>
						<th class="buttons tr" key="Remove"></th>
						
					</tr>
				</thead>

				<tbody class="tbody_listusers" id="tableHolder">
					<?php
					
					$sql = "";
					$companyID = $_SESSION['companyID'];
					
					if(isset($_SESSION['userroleid']))
					{
						switch($_SESSION['userroleid'])
						{
							case 1:
								if($seeHidden == "1")
								{
									$stmt = $con->prepare('SELECT User.ID, User.Name, User.Login, Company.Name AS CompanyName, User.ResetKey AS Activated, Company.ID AS CompanyID, User.Active, User.Hidden FROM Company INNER JOIN User ON User.CompanyID = Company.ID WHERE User.UserRoleID = ? GROUP BY CompanyID');
									$stmt->bind_param('i', $userroleToShow);
								}
								else
								{
									$stmt = $con->prepare('SELECT User.ID, User.Name, User.Login, Company.Name AS CompanyName, User.ResetKey AS Activated, Company.ID AS CompanyID, User.Active, User.Hidden FROM Company INNER JOIN User ON User.CompanyID = Company.ID WHERE User.UserRoleID = ? AND User.Hidden = 0 GROUP BY CompanyID');
									$stmt->bind_param('i', $userroleToShow);
								}
								break;
							
							case 2:
								if($seeHidden == "1")
								{
									$stmt = $con->prepare('SELECT User.ID, User.Name, User.Login, Company.Name AS CompanyName, User.ResetKey AS Activated, Company.ID AS CompanyID, User.Active, User.Hidden FROM Company INNER JOIN User ON User.CompanyID = Company.ID WHERE User.UserRoleID = ? AND User.CompanyID = ?');
									$stmt->bind_param('ii', $userroleToShow, $companyID);
								}
								else
								{
									$stmt = $con->prepare('SELECT User.ID, User.Name, User.Login, Company.Name AS CompanyName, User.ResetKey AS Activated, Company.ID AS CompanyID, User.Active, User.Hidden FROM Company INNER JOIN User ON User.CompanyID = Company.ID WHERE User.UserRoleID = ? AND User.CompanyID = ? AND User.Hidden = 0');
									$stmt->bind_param('ii', $userroleToShow, $companyID);
								}
							break;
							
							default:
							break;
						}
					} 
					
					$stmt->execute();
					
					$result = $stmt->get_result();
					
					while($row = $result->fetch_assoc())
					{
					

						echo "<tr>";
						echo "<td class='name'>";
						echo $row['CompanyName'];
						echo "</td>";
						echo "<td class='business'>";
						echo $row['Name'];
						echo "</td>";
						echo "<td class='email'>";
						echo $row['Login'];
						echo "</td>";
						if($userroleToShow == 3)
						{
							echo "<td class='buttons'>";
							echo "<a href='#' ><div class='list_button_show' onclick=\"javascript:location.href='showEmployee.php?ID=" . $row['ID'] . "'\" ></div></a>";
							echo "</td>";
							echo "<td class='buttons'>";
							echo "<a href='editEmployee.php?ID=" . $row['ID'] . "' ><div class='list_button_edit' ></div></a>";
							echo "</td>";
						}
						else
						{
							echo "<td class='buttons'>";
							echo "<a href='editCompany.php?ID=" . $row['CompanyID'] . "' ><div class='list_button_edit' ></div></a>";
							echo "</td>";
						}
						echo "<td class='buttons'>";
						if($seeHidden == "1")
						{
							if($row['Hidden'] == 1)
								echo "<a href='#' ><div class='list_button_hidden' id=\"text1\" onMouseOver=\"show(getText('ButtonHidden'))\" onMouseOut=\"hide()\"  onclick='SalesMakeover.ReShowRow(" . $row['ID'] . ", \"" . $row['Name'] . "\")' ></div></a>";
							else
							{
								if($row['Activated'] != null)
								{
									echo "<div class='list_button_pending' id=\"text1\" onMouseOver=\"show(getText('ButtonYellow'))\" onMouseOut=\"hide()\" onclick=\"SalesMakeover.SendMail('" . $row['Login'] . "', '" . $row['Name'] . "', '" . $row['Activated'] . "')\" ><input type=\"hidden\" value=" . $row['Active'] . "></div>";
										
										

								}
								else
								{
									switch($row['Active'])
									{
										case 0:
											echo "<div class='list_button_active' id=\"text1\" onMouseOver=\"show(getText('ButtonRed'))\" onMouseOut=\"hide()\" onclick='SalesMakeover.ChangeActive(" . $row['ID'] . ")' ><input type=\"hidden\" value=" . $row['Active'] . "></div>";
										
										break;
										case 1:
											echo "<div class='list_button_active_on' id=\"text1\" onMouseOver=\"show(getText('ButtonGreen'))\" onMouseOut=\"hide()\" onclick='SalesMakeover.ChangeActive(" . $row['ID'] . ")' ><input type=\"hidden\" value=" . $row['Active'] . "></div>";
										break;
									}
								}
							}
						
						
						}
						else
						{
							if($row['Activated'] != null)
							{
								echo "<div class='list_button_pending' id=\"text1\" onMouseOver=\"show(getText('ButtonYellow'))\" onMouseOut=\"hide()\" onclick=\"SalesMakeover.SendMail('" . $row['Login'] . "', '" . $row['Name'] . "', '" . $row['Activated'] . "')\" ><input type=\"hidden\" value=" . $row['Active'] . "></div>";
									
									

							}
							else
							{
								switch($row['Active'])
								{
									case 0:
										echo "<div class='list_button_active' id=\"text1\" onMouseOver=\"show(getText('ButtonRed'))\" onMouseOut=\"hide()\" onclick='SalesMakeover.ChangeActive(" . $row['ID'] . ")' ><input type=\"hidden\" value=" . $row['Active'] . "></div>";
									
									break;
									case 1:
										echo "<div class='list_button_active_on' id=\"text1\" onMouseOver=\"show(getText('ButtonGreen'))\" onMouseOut=\"hide()\" onclick='SalesMakeover.ChangeActive(" . $row['ID'] . ")' ><input type=\"hidden\" value=" . $row['Active'] . "></div>";
									break;
								}
							}
						}
						echo "</td>";						
						echo "<td class='buttons'>";
						if($row['Hidden'] == 0)
							echo "<a href='#' ><div class='list_button_remove'  onclick='SalesMakeover.DeleteRow(" . $row['ID'] . ", \"" . $row['Name'] . "\")' ></div></a>";
						echo "</td>";
						
						echo "</tr>";
					}
					
					//mysqli_close($con);
					
					?>
				</tbody>
			</table>
			</div
		</div>
			

	</div>
	<?php if(isset($_SESSION["Popup"]) && isset($_SESSION["MessageKey"])){ $message = $_SESSION['Popup'];$message2=$_SESSION['MessageKey']; echo "<script type='text/javascript'>setTimeout(function() { alert('$message '+getText('$message2')); }, 10.2);</script>"; unset($_SESSION['Popup']); unset($_SESSION['MessageKey']);} ?>




	<div class="OVER" id="form_editEmployee" >
		<div class="overlayAddEmployee">
			<div class="overlay_text"style="height:50px;">
				<b><?php echo ( isset($_SESSION['companyName']) && $_SESSION['companyName'] != '') ? $_SESSION['companyName'] : '';?></b>
				<br>
				<div class="tr" key="addEmployee_new">Titsssle</div>
			</div>

			<form id="addEmployee" method="post" action="adding.php?id=employee" accept-charset="UTF-8" onsubmit="return SalesMakeover.validateAddEmployeeForm()">
				<?php  echo "<input type=\"hidden\" name=\"companyID\" value=\"" . $companyID . "\">" ?>
		
				<div class="editCompanyAddManagerLabel tr" key="Name" style="margin-top:15px;"></div>
				<input type="text" id="newemployeename" name="newemployeename" >
				<div class="editCompanyAddManagerLabel tr" key="Email" ></div>
				<input type="email" id="newemployeeemail" name="newemployeeemail">
				<div class="editCompanyAddManagerLabel tr" key="addEmployee_manager" style="margin-top:15px;"></div>
				<select name="supervisorID" style="border: 1px solid #9c9c9c;"><option selected disabled>-----</option>
				<?php  
					$companyID = $_SESSION['companyID'];
					$stmt2 = $con->prepare('SELECT User.ID, User.Name FROM User WHERE CompanyID = ? AND UserRoleID = 2');
					$stmt2->bind_param('i', $companyID);
					$stmt2->execute();
				
					//$sql2 = "SELECT User.ID, User.Name FROM User WHERE CompanyID = '$companyID' AND UserRoleID = 2";
					//$result2 = mysqli_query($con,$sql2);
					$result2 = $stmt2->get_result();
					while($row = $result2->fetch_assoc())
					{
						echo "<option value=\"" . $row['ID'] . "\">" . $row['Name'] . "</option>";
					}
				
					mysqli_close($con);
				?>
				</select>
				<div class="form_add_buttons" >
					<div class="form_add_left_align" id=\"button\" onclick="SalesMakeover.hideDiv('form_editEmployee',true)">
					<input type="button" class="button_black tr" key="Exit" value="'+exit+'">
				</div>
				<div class="form_add_right_align" id=\"button\">
					<input type="submit" class="button tr"  key="Add">
				</div>
				</div>
			</form>
		</div>
	</div>
			





  

<div class="Effect" id="ButtinInfo">Div 1 Content</div>
<footer>
<div class = 'WaterMark'>© 2014 Sales Makeover</div>
</footer>
</body>

</html>
<script type="text/javascript" charset="UTF-8">
var cursorX;
var cursorY;
document.onmousemove = function(e){
    cursorX = e.pageX;
    cursorY = e.pageY;
}
function show(text) 
{
	var d = document.getElementById('ButtinInfo');
	
	
	d.style.visibility = "visible";
	d.style.color = "gray";
	d.innerHTML = text;
	
	d.style.left = (cursorX-d.offsetWidth*0.5)+'px';
	d.style.top = (cursorY-d.offsetHeight-35)+'px';
}
function hide()
{
	var d = document.getElementById('ButtinInfo');
	d.style.visibility = "hidden";
}

$(document).ready(function() 
{ 
	var showHidden = "<?php echo ( isset($_SESSION['SeeHidden']) && $_SESSION['SeeHidden'] != '') ? $_SESSION['SeeHidden'] : '';?>";
	if(showHidden == "1")
		$("#seeHidden").prop('checked', true);
		
	SalesMakeover.doListUserResize();
	window.onresize = SalesMakeover.doListUserResize;
	
	$("#myTable").tablesorter({
		widgets: ["zebra"],
		widgetOptions : {
			zebra : [ "even", "odd" ]
		}
	});
	
	
}); 



function Show()
{
var d = document.getElementById('ButtinInfo');
d.slideToggle('slow');
}


</script>